package com.guaju.yueshangLife.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.CategoryActivity;
import com.guaju.yueshangLife.activity.MainActivity;
import com.guaju.yueshangLife.activity.mine.OrderDetailActivity;
import com.guaju.yueshangLife.adapter.NoneCommonRecyclerViewAdapter;
import com.guaju.yueshangLife.base.BaseFragment;
import com.guaju.yueshangLife.bean.CategoryTabBean;
import com.guaju.yueshangLife.bean.CategoryTotal;
import com.guaju.yueshangLife.databinding.FragmentCategoryBinding;
import com.guaju.yueshangLife.event.CodeEvent;
import com.guaju.yueshangLife.net.ApiClient;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.ToastUtil;
import com.guaju.yueshangLife.utils.Utils;
import com.guaju.yueshangLife.viewmodel.CategoryVM;
import com.guaju.yueshangLife.widget.ItemCategoryTab;
import com.guaju.yueshangLife.widget.SeperateLine;
import com.zhy.autolayout.AutoLinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import rx.Observable;

/**
 * Created by guaju on 2018/9/21.
 */

public class CategoryFragment extends BaseFragment {

    FragmentCategoryBinding binding;
    private LayoutInflater layoutInflater;
    private NoneCommonRecyclerViewAdapter<String> namesAdapter;
    ArrayList<CategoryTotal.DataBean.ChildrenBeanX.ChildrenBean> lists = new ArrayList<>();
    ItemCategoryTab[] tabs;
    private MainActivity activity;
    private ArrayList<CategoryTotal.DataBean.ChildrenBeanX> secondCate = new ArrayList<>();
    private ArrayList<ArrayList<CategoryTabBean>> thirdCate = new ArrayList<>();
    private ItemCategoryTab item_tab;
    ArrayList<RadioButton> radioButtons = new ArrayList<>();
    Map<String, View> rightView = new HashMap<>();
    Map<CategoryTabBean, ItemCategoryTab> item_tabs = new HashMap<>();
    private View news_loading;


    @Override
    public int setContent() {
        return R.layout.fragment_category;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        binding = (FragmentCategoryBinding) bindingView;
        EventBus.getDefault().register(this);
        prepareView();
        showContentView();
        activity = (MainActivity) getActivity();

    }


    private void prepareView() {
        layoutInflater = LayoutInflater.from(getActivity());
        CategoryVM categoryVM = new CategoryVM(binding);

        binding.setViewModle(categoryVM);
        binding.categoryAll.setChecked(true);
        rightView.put("高端定制", binding.sdvMake);
    }


    @Override
    protected void loadData() {
        ApiEntity.category();
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onContentReceived(CategoryTotal categoryTotal) {
        if (categoryTotal != null) {
            CategoryTotal.DataBean dataBean = categoryTotal.getData().get(0);

            List<CategoryTotal.DataBean.ChildrenBeanX> children = dataBean.getChildren();
            for (CategoryTotal.DataBean.ChildrenBeanX x : children) {
                secondCate.add(x);
                List<CategoryTotal.DataBean.ChildrenBeanX.ChildrenBean> children1 = x.getChildren();
                ArrayList<CategoryTabBean> names = new ArrayList<>();
                for (CategoryTotal.DataBean.ChildrenBeanX.ChildrenBean child : children1) {
                    CategoryTotal.DataBean.ChildrenBeanX.ChildrenBean.CoverBeanXX cover = child.getCover();
                    CategoryTabBean categoryTabBean = new CategoryTabBean(child.getId(), child.getName(), cover == null ? "" : (cover.getHost() + cover.getPath()));
                    names.add(categoryTabBean);
                    lists.add(child);
                }
                thirdCate.add(names);
            }
            updateView();
        }


    }

    private void updateView() {

        /**
         * left
         */
        for (CategoryTotal.DataBean.ChildrenBeanX second : secondCate) {
            RadioButton radioButton = (RadioButton) layoutInflater.inflate(R.layout.cate_radio_item, binding.radiogroup, false);
            radioButton.setText(second.getName());
            binding.radiogroup.addView(radioButton);
            radioButtons.add(radioButton);
        }
        /**
         * right
         * ① 先进行块
         * ② 进行安排
         */
        //①
        for (int i = 0; i < thirdCate.size(); i++) {
            AutoLinearLayout cate_right_item = (AutoLinearLayout) layoutInflater.inflate(R.layout.cate_right_item, binding.llright, false);
            SeperateLine seperateLine = cate_right_item.findViewById(R.id.seperateline);

            AutoLinearLayout ll_right_tab1 = cate_right_item.findViewById(R.id.ll_right_tab1);
            AutoLinearLayout ll_right_tab2 = cate_right_item.findViewById(R.id.ll_right_tab2);
            AutoLinearLayout ll_right_tab3 = cate_right_item.findViewById(R.id.ll_right_tab3);
            AutoLinearLayout ll_right_tab4 = cate_right_item.findViewById(R.id.ll_right_tab4);
            ll_right_tab2.setVisibility(View.GONE);
            ll_right_tab3.setVisibility(View.GONE);
            ll_right_tab4.setVisibility(View.GONE);

            String name = secondCate.get(i).getName();
            seperateLine.setText(name);

            if (name.contains("定制")) {
                CategoryTotal.DataBean.ChildrenBeanX.CoverBeanX cover = secondCate.get(i).getCover();
                String sdvurl = (cover == null ? "" : cover.getHost() + cover.getPath());
                binding.sdvMake.setImageURI(sdvurl);
                binding.sdvMake.setVisibility(View.GONE);
            }

            for (int j = 0; j < thirdCate.get(i).size(); j++) {
                if (j < 3) {
                    item_tab = (ItemCategoryTab) layoutInflater.inflate(R.layout.cate_right_item_tab, ll_right_tab1, false);
                    CategoryTabBean tab = thirdCate.get(i).get(j);
                    item_tab.setIconAndTitle(tab);
                    ll_right_tab1.addView(item_tab);
                    item_tabs.put(tab, item_tab);
                } else if (j > 2 && j < 6) {
                    item_tab = (ItemCategoryTab) layoutInflater.inflate(R.layout.cate_right_item_tab, ll_right_tab2, false);
                    CategoryTabBean tab = thirdCate.get(i).get(j);
                    item_tab.setIconAndTitle(tab);
                    ll_right_tab2.addView(item_tab);
                    ll_right_tab2.setVisibility(View.VISIBLE);
                    item_tabs.put(tab, item_tab);
                } else if (j > 5 && j < 9) {
                    item_tab = (ItemCategoryTab) layoutInflater.inflate(R.layout.cate_right_item_tab, ll_right_tab3, false);
                    CategoryTabBean tab = thirdCate.get(i).get(j);
                    item_tab.setIconAndTitle(tab);
                    ll_right_tab3.addView(item_tab);
                    ll_right_tab3.setVisibility(View.VISIBLE);
                    item_tabs.put(tab, item_tab);
                } else if (j > 8 && j < 12) {
                    item_tab = (ItemCategoryTab) layoutInflater.inflate(R.layout.cate_right_item_tab, ll_right_tab4, false);
                    CategoryTabBean tab = thirdCate.get(i).get(j);
                    item_tab.setIconAndTitle(tab);
                    ll_right_tab4.addView(item_tab);
                    ll_right_tab4.setVisibility(View.VISIBLE);
                    item_tabs.put(tab, item_tab);
                }
            }
            //②
            ArrayList<CategoryTabBean> categoryTabBeans = thirdCate.get(i);
            if (categoryTabBeans == null || categoryTabBeans.isEmpty()) {

            } else {
                binding.llright.addView(cate_right_item);
                rightView.put(secondCate.get(i).getName(), cate_right_item);
            }
        }
        setRadioButtonListener();
        setItemTabListener();
    }

    private void setItemTabListener() {
        Set<Map.Entry<CategoryTabBean, ItemCategoryTab>> entries =
                item_tabs.entrySet();

        for (Map.Entry<CategoryTabBean, ItemCategoryTab> entry : entries) {
            final CategoryTabBean key = entry.getKey();
            ItemCategoryTab value = entry.getValue();
            value.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(CategoryActivity.TAB_TYPE, key.getId());
                    Intent intent = new Intent(getActivity(), CategoryActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        }
        binding.sdvMake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show("高端定制服务请电联客服");
            }
        });

    }

    private void setRadioButtonListener() {


//        for (int i = 0; i < radioButtons.size(); i++) {
//            radioButtons.get(i).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    if (isChecked) {
//                        String trim = buttonView.getText().toString().trim();
//                        if (trim.contains("会员") || TextUtils.equals("全部分类", trim)) {
//                            setAllHide(false);
//                            binding.sdvMake.setVisibility(View.GONE);
//                        } else {
//                            setAllHide(true);
//                            rightView.get(trim).setVisibility(View.VISIBLE);
//                        }
//                    }
//                }
//            });
//        }
        binding.radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton buttonView = binding.radiogroup.findViewById(checkedId);
                if (buttonView.isChecked()) {
                    String trim = buttonView.getText().toString().trim();
                    if (trim.contains("会员") || TextUtils.equals("全部分类", trim)) {
                        setAllHide(false);
                        binding.sdvMake.setVisibility(View.GONE);
                    } else {
                        setAllHide(true);
                        rightView.get(trim).setVisibility(View.VISIBLE);
                    }
                }
            }
        });


    }

    private void setAllHide(boolean value) {

        for (int i = 0; i < rightView.size(); i++) {
            View rightChild = binding.llright.getChildAt(i);
            if (value) {
                rightChild.setVisibility(View.GONE);
            } else {
                rightChild.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
//        activity.setTab_position(1);
    }
}
